package com.shixun.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shixun.domain.Helper;
import com.shixun.domain.Student;
import com.shixun.dto.HelperDto;
import com.shixun.service.HelperService;
import com.shixun.service.StudentService;
import com.shixun.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/helper")
public class HelperController {
    @Autowired
    HelperService helperService;
    @Autowired
    StudentService studentService;

    /**
     * 条件分页查询
     * @param pageSize
     * @param pageNum
     * @param sname
     * @param sno
     * @return
     */
    @GetMapping("/page")
   public R helperPage(int pageSize,int pageNum,String sname,String sno){
        R r = helperService.helperPage(pageSize, pageNum, sname, sno);
        return r;
   }

    /**
     * 新增
     * @param student
     * @return
     */
    @PostMapping("/addHelper")
   public R addHelper(@RequestBody Student student){

        LambdaQueryWrapper<Helper> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Helper::getSid,student.getId());
        Helper one = helperService.getOne(queryWrapper);
        if(one!=null){
            return new R(300,"该学生已是助手",null);
        }
        Helper helper=new Helper();
        helper.setState(1);
        helper.setSid(student.getId());
        //System.out.println("1111111111111:::"+helper.getSid());
       helperService.saveOrUpdate(helper);
       return new R(200,"添加成功",null);
   }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/del")
    public R delHelper(@RequestParam List<Integer> ids){
        helperService.removeByIds(ids);
        return new R(200,"删除成功",null);
    }
    @DeleteMapping("/delHelper/{id}")
    public R delHelper(@PathVariable Long id){
        helperService.removeById(id);
        return new R(200,"删除成功",null);
    }

    /**
     * 修改任用状态
     * @param
     * @return
     */
    @PutMapping("/updateState")
    public R updateState(@RequestBody HelperDto helperDto){
        Helper helper=new Helper();
        BeanUtils.copyProperties(helperDto,helper);
        helperService.updateById(helper);
        return new R(200,"修改成功",null);
    }
    /**
     *
     */
    @GetMapping("/listStudent")
    public R listStudent(){
        List<Student> list = studentService.list();
        return new R(200,"",list);
    }

    /**
     * 判断该学生是否为助手
     * @param sid
     * @return
     */
    @GetMapping("/checkIsHelper")
    public R getSession(Long sid){
        if (sid==null){
            return new R(500,"请先登录",null);
        }
        LambdaQueryWrapper<Helper> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Helper::getSid,sid);
        Helper one = helperService.getOne(queryWrapper);
        if(one==null||one.getState()==0){//不是助手或者状态为0
            return  new R(500,"你没有权限查寝",false);
        }
        return new R(200,"",true);
    }
}
